
<%@page import="java.util.HashMap"%>
<%@page import="java.io.FileOutputStream"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.List"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="java.io.File"%>
<%
	String clientContext = request.getContextPath();
	File file;
	ServletContext context = pageContext.getServletContext();
	String contentType = request.getContentType();

	if ((contentType.indexOf("multipart/form-data") >= 0)) {
		DiskFileItemFactory factory = new DiskFileItemFactory();
		// 	      factory.setSizeThreshold(maxMemSize);
		factory.setRepository(new File("c:\\tempUpload"));

		ServletFileUpload upload = new ServletFileUpload(factory);

		try {
			HashMap<String, List<FileItem>> map = new HashMap<String, List<FileItem>>();
			map.put("items", upload.parseRequest(request));
			List<FileItem> fileItems = map.get("items");
			Iterator<FileItem> i = fileItems.iterator();

			while (i.hasNext()) {
				FileItem fi = (FileItem) i.next();
				if (!fi.isFormField()) {
					String fileName = fi.getName();

					if (fileName.contains(".osm")
							|| fileName.contains(".txt")) {
						String filePath = "D:\\";
						byte[] data = fi.get();
						if (fileName.lastIndexOf("\\") >= 0) {
							filePath += fileName.substring(fileName
									.lastIndexOf("\\"));
							file = new File(filePath);
						} else {
							filePath += fileName.substring(fileName
									.lastIndexOf("\\") + 1);
							file = new File(filePath);
						}
						out.print(filePath);
						fi.write(file);
						response.sendRedirect(clientContext
								+ "/TEST/client.jsp?name="
								+ fi.getName());
					} else {
						response.sendRedirect(clientContext
								+ "/TEST/client.jsp?error=" + (1));
					}
				}
			}
		} catch (Exception ex) {
			System.out.println(ex);
			response.sendRedirect(clientContext
					+ "/TEST/client.jsp?error=" + ex.getMessage());
		}
	} else {
		response.sendRedirect(clientContext + "/TEST/client.jsp?error="
				+ 3);
	}
%>